/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.common;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MediatePropertyInfo {

  private String propertyName;
  private boolean isChildEntity = false;
  private IGspCommonField element;
  private IGspCommonDataType node;
  private TypeInfo type;
  private UnifiedDataTypeDef udt;

  public MediatePropertyInfo(String propertyName) {
    this.propertyName = propertyName;
  }

  public MediatePropertyInfo(IGspCommonField element){
    this(element.getLabelID());
    this.element = element;
  }

  public boolean getIsChildEntity() {
    return this.isChildEntity;
  }

  public void setIsChildEntity(boolean value) {
    this.isChildEntity = value;
  }

  public String getSetterMethodName() {
    return Utils.getSetterMethodName(this.propertyName);
  }

  public String getGetterMethodName() {
    return Utils.getGetterMethodName(this.propertyName);
  }

  public String getPropertyName() {
    return this.propertyName;
  }

  public Boolean canWrite() {
    return true;
  }

  public TypeInfo getPropertyType() {
    return this.type;
  }

  public MediateType getMediateType(){
    if(getPropertyType() == null){
      return null;
    }
    return new MediateType(getPropertyType().getTypePackageName(), getPropertyType().getTypeName());
  }

  @Deprecated
  public TypeInfo getReturnType(){return getPropertyType();}

  public void setPropertyType(TypeInfo type) {
    this.type = type;
  }

  public <T extends IGspCommonField> T getElement() {
    return (T) this.element;
  }

  public void setElement(IGspCommonField element) {
    this.element = element;
  }

  public <T extends IGspCommonDataType> T getNode() {
    return (T) this.node;
  }

  public void setNode(IGspCommonDataType node) {
    this.node = node;
  }

  private Map<String, Object> extendInfos;

  public void setExtendInfo(String key, Object value) {
    if (extendInfos == null) {
      extendInfos = new HashMap<>();
    }
    extendInfos.put(key, value);
  }

  public Map<String, Object> getExtendInfos() {
    return extendInfos == null ? Collections.emptyMap() : extendInfos;
  }

  public <T> T getExtendInfo(String key) {
    if (extendInfos == null) {
      return null;
    }
    return (T) extendInfos.get(key);
  }

  public void setUdt(UnifiedDataTypeDef udt) {
    this.udt = udt;
  }

  public UnifiedDataTypeDef getUdt(){
    return udt;
  }
}
